package com.chat.demo.userservice.Entity.DAO;

import com.chat.demo.userservice.DTO.message.MessageDTO;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface MessageDao {

    // 发送私聊消息
    @Insert("INSERT INTO app.message (sender_id, receiver_type, receiver_id, content, status) " +
            "VALUES (#{senderId}, #{receiverType}, #{receiverId}, #{content}, 'sent')")
    @Options(useGeneratedKeys = true, keyProperty = "messageId")
    void sendMessage(MessageDTO message);

    // 更新消息状态
    @Update("UPDATE app.message " +
            "SET status = #{status} " +
            "WHERE message_id = #{messageId}")
    void updateMessageStatus(@Param("messageId") Long messageId, @Param("status") String status);

    // 查询两个用户之间的私聊记录
    @Select("SELECT * FROM app.message " +
            "WHERE receiver_type = 'user' " +
            "AND ((sender_id = #{userId1} AND receiver_id = #{userId2}) " +
            "OR (sender_id = #{userId2} AND receiver_id = #{userId1})) " +
            "ORDER BY sent_at")
    List<MessageDTO> getPrivateChatHistory(@Param("userId1") Long userId1, @Param("userId2") Long userId2);
}